//
//  HomepageViewController.m
//  Sunexpress
//
//  Created by 筱亮 on 2017/5/8.
//  Copyright © 2017年 曹亮. All rights reserved.
//

#import "HomepageViewController.h"
#import "TheMacro.h"

@interface HomepageViewController ()<AMapLocationManagerDelegate,MAMapViewDelegate>
{
    
}
/** 地理编码管理器 */
@property (nonatomic, strong) CLGeocoder *geoC;
@property(nonatomic , strong) AMapLocationManager *locationManager;
@property(nonatomic , strong) MAPointAnnotation *annotation;
@property(nonatomic , strong) MAMapView *mapView;
@end

@implementation HomepageViewController

-(void)viewWillAppear:(BOOL)animated
{
    self.mapView.delegate = self;
    self.locationManager.delegate = self;
    [super viewWillAppear:animated];
}
-(void)viewDidDisappear:(BOOL)animated
{
    self.mapView.delegate = nil;
    self.locationManager.delegate = nil;
    [self.mapView removeFromSuperview];
    [super viewDidDisappear:animated];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    if (_tagString == 0 || _tagString == 1 || _tagString == 2) {
        //添加地图
//        [self myMapView];
    }else if (_tagString == 3) {
        [self.view removeFromSuperview];
        self.view.backgroundColor = [UIColor orangeColor];
    }
    
}

//获取商铺信息
-(void)shopNet:(NSString *)lng lat:(NSString *)lat
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                lng,@"lng",
                                lat,@"lat",
                                @"2",@"diner_type",  //1餐车2早餐店
                                @"1",@"is_delivery"  //1为可配送
                                , nil];
    [NetWorkingRequest postWithURLString:STOREINFORMATION parameters:dic success:^(NSDictionary *data) {
        
        NSLog(@"=========%@",data);
        
        
    } failure:^(NSError *error) {
        
        
    }];
}
-(void)myMapView
{
    ///地图需要v4.5.0及以上版本才必须要打开此选项（v4.5.0以下版本，需要手动配置info.plist）
    [AMapServices sharedServices].enableHTTPS = YES;
    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height-40)];
    //初始化地图
    self.mapView.mapType = MAMapTypeStandard;
    ///如果您需要进入地图就显示定位小蓝点，则需要下面两行代码
    self.mapView.showsUserLocation = YES;
    self.mapView.userTrackingMode = MAUserTrackingModeFollow;
    self.mapView.zoomEnabled = YES;    //NO表示禁用缩放手势，YES表示开启
    self.mapView.scrollEnabled = YES;    //NO表示禁用滑动手势，YES表示开启
    self.mapView.rotateEnabled= YES;    //NO表示禁用旋转手势，YES表示开启
    self.mapView.rotateCameraEnabled= NO;    //NO表示禁用倾斜手势，YES表示开启
    //把地图添加至view
//    [self.view addSubview:self.mapView];
    [self.locationManager startUpdatingLocation];
    
}
#pragma mark --AMapLocationManagerDelegate
- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error
{
    //定位错误
    NSLog(@"%s, amapLocationManager = %@, error = %@", __func__, [manager class], error);
}

//可在此添加大头针，更新地图。
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
{
    NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
    
    if (!self.annotation) {
        self.annotation = [[MAPointAnnotation alloc] init];
    }
    self.annotation.coordinate = location.coordinate;
    [self.mapView addAnnotation:self.annotation];
    self.annotation.title = reGeocode.city;
    self.annotation.subtitle = reGeocode.AOIName;
//    self.showLabel.text = reGeocode.AOIName;
    
    [self.mapView setCenterCoordinate:location.coordinate];
    [self.mapView setZoomLevel:15.1 animated:NO];
    if (reGeocode)
    {
        NSLog(@"reGeocode:%@", reGeocode);
        [self shopNet:[NSString stringWithFormat:@"%f",location.coordinate.longitude] lat:[NSString stringWithFormat:@"%f",location.coordinate.latitude]];
        [self.locationManager stopUpdatingLocation];
    }
}
#pragma mark - 懒加载
/** 地理编码管理器 */
- (CLGeocoder *)geoC
{
    if (!_geoC) {
        _geoC = [[CLGeocoder alloc] init];
    }
    return _geoC;
}

-(AMapLocationManager *)locationManager
{
    if (!_locationManager) {
        _locationManager = [[AMapLocationManager alloc] init];
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
        //带逆地理信息的一次定位（返回坐标和地址信息）
        //定位超时时间，最低2s，此处设置为10s
        _locationManager.locationTimeout =10;
        //逆地理请求超时时间，最低2s，此处设置为10s
        _locationManager.reGeocodeTimeout = 10;
        //带逆地理定位
        [_locationManager setLocatingWithReGeocode:YES];
    }
    return _locationManager;
}

-(void)mapViewRegionChanged:(MAMapView *)mapView
{
    [self.mapView removeFromSuperview];
    [self.view addSubview:mapView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
